package com.example.demo2.controller;

import com.example.demo2.entity.*;
import com.example.demo2.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@RestController
@CrossOrigin
public class EquipmentController {

    @Autowired
    EquipmentMapper equipmentMapper;
    @Autowired
    OrdersMapper ordersMapper;
    @Autowired
    OrdersDetailMapper ordersDetailMapper;
    @Autowired
    SolutionMapper solutionMapper;
    @Autowired
    ProjectMapper projectMapper;
    @Autowired
    StandardMapper standardMapper;

    private ConcurrentHashMap<String, Integer> concurrentMap;

    @Autowired
    public void MyController(ConcurrentHashMap<String, Integer> concurrentMap) {
        this.concurrentMap = concurrentMap;
    }

    // 检测人员所有设备清单
    @GetMapping("/findOrdersByUID")
    public List<Orders> findOrdersByUID() {
        Integer uID = concurrentMap.get("uID");
        List<Orders> orderss = ordersMapper.findOrdersByUID(uID);
        for (Orders orders : orderss) {
            List<OrdersDetail> details = ordersDetailMapper.findOrdersDetailByOId(orders.getoId());
            for (OrdersDetail ordersDetail : details) {
                //解决方案名称
                Solution solution = solutionMapper.findSolutionBySoId(ordersDetail.getSoId());
                if(solution != null) {
                    ordersDetail.setSoName(solution.getSoName());
                }
                //设备名称
                Equipment equipment = equipmentMapper.findEquipmentByeId(ordersDetail.getEId());
                if(equipment != null) {
                    ordersDetail.setEName(equipment.geteName());
                }
                //项目名称、GB
                Project project = projectMapper.findProjectByPjId(ordersDetail.getPjId());
                if(project != null) {
                    ordersDetail.setPjName(project.getPjName());
                    ordersDetail.setPjGB(project.getPjGB());
                    Standard standard = standardMapper.findStandardByStaId(project.getStaId());
                    if(standard != null) {
                        ordersDetail.setStaName(standard.getStaName());
                    }
                }
            }
            orders.setDetails(details);
        }
        return orderss;
    }

    @GetMapping("/findOrdersNumByUID")
    public int findOrdersNumByUID() {
        Integer uID = concurrentMap.get("uID");
        List<Orders> orderss = ordersMapper.findOrdersByUID(uID);
        return orderss.size();
    }

    // 删除设备清单
    @PutMapping("/deleteOrders/{oId}")
    public int deleteOrders(@PathVariable int oId) {
        System.out.println(oId);
        return ordersMapper.deleteOrdersByOID(oId) & ordersDetailMapper.deleteOrdersDetailByOId(oId);
    }

    @RequestMapping("/EquipmentList")
    public List<Equipment> equitment(@RequestParam int pjId){
        List<Equconnect> clist = equipmentMapper.FindEquconnectByPjid(pjId);
        if(clist != null){
            List<Equipment> elist = new ArrayList<>();
            for(Equconnect one : clist){
                Equipment ep = equipmentMapper.FindEquipmentById(one.geteId());
                elist.add(ep);
            }
            return elist;
        } else {
            return null;
        }
    }

    // 不再使用 session 来判断登录状态，直接用 concurrentMap
    @GetMapping("/logincommit")
    public String logincommit() {
        if(concurrentMap.get("uID") != null) {
            return "已经登陆";
        } else {
            return "请登录";
        }
    }

    @RequestMapping("/EquipmentDetail")
    public List<Solution> AllSolution(@RequestParam int eId){
        return equipmentMapper.FindSolutionByEid(eId);
    }

    // 去掉 HttpSession 参数
    @PutMapping("/AddEquipmentIntoOrders")
    public String AddEquipmentIntoOrders(@RequestBody Map<String,String> param) {
        Integer soId = Integer.parseInt(param.get("soId"));
        Integer eId = Integer.parseInt(param.get("eId"));
        Integer uID = concurrentMap.get("uID");

        Orders order = new Orders();
        com.example.demo.entity.OrderDetail od = new com.example.demo.entity.OrderDetail();
        order.setuID(uID);
        order.setoDate(String.valueOf(LocalDateTime.now()));
        order.setoStatus("计划采购");

        int flag = equipmentMapper.AddSolutionIntoOrders(order);
        if(flag == 1) {
            od.setEId(eId);
            System.out.println(order.getoId());
            od.setOId(order.getoId());
            od.setPjId(equipmentMapper.FindEquconnectByEid(eId).get(0).getPjId());
            od.setSoId(soId);
            equipmentMapper.AddOrderDetail(od);
            return "置入成功";
        } else {
            return "置入清单失败";
        }
    }

    @RequestMapping("/getEquOfProject")
    public List<Equipment> getEquOfProject(@RequestParam int pjid) {
        List<Equipment> equipments = new ArrayList<>();
        List<Integer> eids = equipmentMapper.findEquIdOfProject(pjid);
        if(eids != null) {
            for (int eid : eids) {
                Equipment equipment = equipmentMapper.findEquById(eid);
                equipments.add(equipment);
            }
        }
        return equipments;
    }

    @DeleteMapping("/deleteEquOfProject")
    public int deleteEquOfProject(@RequestParam int eid) {
        equipmentMapper.deleteEquOfProject(eid);
        return equipmentMapper.deleteEquipmentOfProject(eid);
    }

    @DeleteMapping("/deleteEquListOfProject")
    public String deleteEquListOfProject(@RequestBody List<Integer> eIds) {
        if (eIds == null || eIds.isEmpty()) {
            return "删除失败：ID列表为空";
        }

        for(Integer id : eIds){
            int result = equipmentMapper.deleteEquOfProject(id);
            if(result!=1) return "删除失败";
        }

        return "删除成功";


//        int result = equipmentMapper.deleteEquListOfProject(eIds);

    }

    @PutMapping("/addEquOfProject")
    public int addEquOfProject(@RequestBody Equipment equipment) {
        int pjId = equipment.getPjId();
        String eName = equipment.geteName();
        String eUnit = equipment.geteUnit();
        Equipment equipment1 = new Equipment();
        Equconnect equconnect = new Equconnect();
        equipment1.seteName(eName);
        equipment1.seteUnit(eUnit);
        equipmentMapper.addEquOfProject(equipment1);
        equconnect.setPjId(pjId);
        equconnect.seteId(equipment1.geteId());
        return equipmentMapper.addEquConnect(equconnect);
    }
}
